package com.example.deertrip.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import com.example.deertrip.bean.Moment;
import com.example.deertrip.bean.Tb_AdminInfo;
import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.bean.Tb_HotelReserve;
import com.example.deertrip.bean.Tb_UserInfo;
import com.example.deertrip.bean.Test;
import com.example.deertrip.bean.Flight;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DBOperate {
    public DBHleper dbhelper;
    private Context context;
    private DBOperate dbOperate;

    public DBOperate(Context context) {

        this.dbhelper=DBHleper.getInstance(context);
        this.context=context;
    }
    //dsds

    public void saveHotelInfo(Tb_HotelInfo HotelInfo){
        SQLiteDatabase db=dbhelper.getWritableDatabase();   //拿到可以操作的数据库
        ContentValues values = new ContentValues();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        Bitmap bmp =HotelInfo.getHotelImage();     //拿到Test中的点阵图
        bmp.compress(Bitmap.CompressFormat.PNG, 100, os);  //点阵图压缩成字节流
        values.put("HotelImage", os.toByteArray());      //字节流存入contentvalues
        values.put("HotelName",HotelInfo.getHotelName());
        values.put("HotelAddress",HotelInfo.getHotelAddress());
        values.put("HotelScore",HotelInfo.getHotelScore());
        values.put("HotelTel",HotelInfo.getHotelTel());
        db.insert("Tb_HotelInfo",null,values);   //通过contentvalues插入表test
    }
    @SuppressLint("Range")
    public List<Tb_HotelInfo> get_All_HotelInfo(){
        List<Tb_HotelInfo> HotelInfoList=new ArrayList<Tb_HotelInfo>();
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Tb_HotelInfo ",null);
        while(cursor.moveToNext()){
            Long HotelTel=cursor.getLong(cursor.getColumnIndex("HotelTel"));
            String HotelName=cursor.getString(cursor.getColumnIndex("HotelName"));
            String HotelAddress=cursor.getString(cursor.getColumnIndex("HotelAddress"));
            String HotelScore=cursor.getString(cursor.getColumnIndex("HotelScore"));
            byte[] in=cursor.getBlob(cursor.getColumnIndex("HotelImage"));//读取字节流图片
            Bitmap bmpout=BitmapFactory.decodeByteArray(in,0,in.length);//字节流转换为点阵

            Tb_HotelInfo HotelInfo=new Tb_HotelInfo(HotelTel,HotelName,bmpout,HotelAddress,HotelScore);
            //构建新Test对象，图片为点阵类型
            HotelInfoList.add(HotelInfo);

        }
        return HotelInfoList;//返回查询到额所有Test对象
    }
    //test
    public void saveHotelReserveInfo(Tb_HotelReserve HotelReserve){
        SQLiteDatabase db=dbhelper.getWritableDatabase();   //拿到可以操作的数据库
        ContentValues values = new ContentValues();

        values.put("HotelName",HotelReserve.getHotelName());
        values.put("RoomNumber",HotelReserve.getRoomNumber());
        values.put("HotelOrderNum",HotelReserve.getHotelOrderNum());
        values.put("HotelOrderPrice",HotelReserve.getHotelOrderPrice());
        values.put("CheckinDate",HotelReserve.getCheckinDate());
        values.put("CheckoutDate",HotelReserve.getCheckoutDate());
        values.put("RoomType",HotelReserve.getRoomType());
        values.put("UserName",HotelReserve.getUserName());
        db.insert("Tb_HotelReserve",null,values);   //通过contentvalues插入表test
    }

    @SuppressLint("Range")
    public List<Tb_HotelReserve> get_All_ReserveInfo(){
        List<Tb_HotelReserve> ReserveInfoList=new ArrayList<Tb_HotelReserve>();

        SQLiteDatabase db=dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Tb_HotelReserve ",null);
        while(cursor.moveToNext()){

            Integer HotelOrderNum=cursor.getInt(cursor.getColumnIndex("HotelOrderNum"));
            int RoomNumber=cursor.getInt(cursor.getColumnIndex("RoomNumber"));
            int HotelOrderPrice=cursor.getInt(cursor.getColumnIndex("HotelOrderPrice"));
            String HotelName=cursor.getString(cursor.getColumnIndex("HotelName"));
            String CheckinDate=cursor.getString(cursor.getColumnIndex("CheckinDate"));
            String CheckoutDate=cursor.getString(cursor.getColumnIndex("CheckoutDate"));
            String RoomType=cursor.getString(cursor.getColumnIndex("RoomType"));
            String UserName=cursor.getString(cursor.getColumnIndex("UserName"));

            Tb_HotelReserve ReserveInfo=new Tb_HotelReserve(RoomNumber,HotelName,HotelOrderPrice,CheckinDate,CheckoutDate,RoomType,UserName,HotelOrderNum);
            ReserveInfoList.add(ReserveInfo);

        }
        return ReserveInfoList;//返回查询到额所有Test对象
    }


    public void saveFlight(Flight flight){
        SQLiteDatabase db=dbhelper.getWritableDatabase();   //拿到可以操作的数据库
        ContentValues values = new ContentValues();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        Bitmap bmp =flight.getCompanyImage();     //拿到Flilght中的点阵图
        bmp.compress(Bitmap.CompressFormat.PNG, 100, os);  //点阵图压缩成字节流
        values.put("CompanyImage", os.toByteArray());//字节流存入contentvalues
        values.put("FlightNumber",flight.getFlightNumber());
        values.put("TakeoffTime",flight.getTakeoffTime());
        values.put("SeatClass",flight.getSeatClass());
        values.put("TicketPrice",flight.getTicketPrice());
        values.put("CompanyName",flight.getCompanyName());
        values.put("LandingTime",flight.getLandingTime());
        values.put("TotalHour",flight.getTotalHour());
        values.put("FlightForm",flight.getFlightForm());
        values.put("FlightTo",flight.getFlightTo());
        db.insert("Flight",null,values);   //通过contentvalues插入表test
    }
    @SuppressLint("Range")
    public List<Flight> get_All_Flight_Data(){
        List<Flight> flightList=new ArrayList<Flight>();
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Flight ",null);
        while(cursor.moveToNext()){
            Integer FlightNumber=cursor.getInt(cursor.getColumnIndex("FlightNumber"));
            String TakeoffTime=cursor.getString(cursor.getColumnIndex("TakeoffTime"));
            String SeatClass=cursor.getString(cursor.getColumnIndex("SeatClass"));
            Integer TicketPrice=cursor.getInt(cursor.getColumnIndex("TicketPrice"));
            String CompanyName=cursor.getString(cursor.getColumnIndex("CompanyName"));
            String LandingTime=cursor.getString(cursor.getColumnIndex("LandingTime"));
            String TotalHour=cursor.getString(cursor.getColumnIndex("TotalHour"));
            String FlightForm=cursor.getString(cursor.getColumnIndex("FlightForm"));
            String FlightTo=cursor.getString(cursor.getColumnIndex("FlightTo"));
            byte[] in=cursor.getBlob(cursor.getColumnIndex("CompanyImage"));//读取字节流图片
            Bitmap bmpout=BitmapFactory.decodeByteArray(in,0,in.length);//字节流转换为点阵

            Flight flight= new Flight(FlightNumber,TakeoffTime,SeatClass,TicketPrice,CompanyName,LandingTime,TotalHour,FlightForm,FlightTo,bmpout);//构建新Test对象，图片为点阵类型
            flightList.add(flight);

        }
        return flightList;//返回查询到额所有Test对象
    }




    public void saveMoment(Moment moment){
        SQLiteDatabase db=dbhelper.getWritableDatabase();   //拿到可以操作的数据库
        ContentValues values = new ContentValues();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        Bitmap bmp =moment.getImage();     //拿到Test中的点阵图
        bmp.compress(Bitmap.CompressFormat.PNG, 100, os);  //点阵图压缩成字节流

        values.put("image", os.toByteArray());      //字节流存入contentvalues
        values.put("username",moment.getUsername());
        values.put("good",moment.getGood());
        values.put("content",moment.getContent());
        db.insert("moment",null,values);   //通过contentvalues插入表test
        db.close();
    }

    @SuppressLint("Range")
    public List<Moment> get_All_Moment_Data(){
        List<Moment> list=new ArrayList<Moment>();
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from moment ",null);
        while(cursor.moveToNext()){
             String username=cursor.getString(cursor.getColumnIndex("username"));
            Integer good=cursor.getInt(cursor.getColumnIndex("good"));
            String content=cursor.getString(cursor.getColumnIndex("content"));
            byte[] in=cursor.getBlob(cursor.getColumnIndex("image"));
            Integer id=cursor.getInt(cursor.getColumnIndex("_id"));
            Bitmap bmpout=BitmapFactory.decodeByteArray(in,0,in.length);

            Moment moment= new Moment(id,content,username,good,bmpout);
           list.add(moment);

        }
        db.close();
        return list;//返回查询到额所有Test对象
    }

    @SuppressLint("Range")
    public List<Moment> get_Hot_Moment_Data(){
        List<Moment> list=new ArrayList<Moment>();
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from moment order by good desc",null);
        while(cursor.moveToNext()){
            String username=cursor.getString(cursor.getColumnIndex("username"));
            Integer good=cursor.getInt(cursor.getColumnIndex("good"));
            String content=cursor.getString(cursor.getColumnIndex("content"));
            byte[] in=cursor.getBlob(cursor.getColumnIndex("image"));
            Integer id=cursor.getInt(cursor.getColumnIndex("_id"));
            Bitmap bmpout=BitmapFactory.decodeByteArray(in,0,in.length);

            Moment moment= new Moment(id,content,username,good,bmpout);
            list.add(moment);

        }
        db.close();
        return list;//返回查询到额所有Test对象
    }
    public void delete_order(Integer id){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        db.execSQL("delete from Tb_HotelReserve where HotelOrderNum=?",new String[]{String.valueOf(id)});
        db.close();
    }


    public void GoodAdd(Integer id){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from moment where _id=?",new String[]{String.valueOf(id)});
        if(cursor.moveToFirst()) {
            @SuppressLint("Range") Integer good = cursor.getInt(cursor.getColumnIndex("good"));
            good = good + 1;
            db.execSQL("update moment set good=? where _id=?", new Object[]{good, id});

        }
        db.close();
    }

    @SuppressLint("Range")
    public List<Tb_HotelInfo> Search_Hotel(String str) {
        List<Tb_HotelInfo> list = new ArrayList<Tb_HotelInfo>();
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from Tb_HotelInfo where (HotelName like '%"+str+"%') or (HotelTel like '%"+str+"%') ", null);
        while (cursor.moveToNext()) {
            Long HotelTel = cursor.getLong(cursor.getColumnIndex("HotelTel"));
            String HotelName = cursor.getString(cursor.getColumnIndex("HotelName"));
            String HotelAddress = cursor.getString(cursor.getColumnIndex("HotelAddress"));
            String HotelScore = cursor.getString(cursor.getColumnIndex("HotelScore"));
            byte[] in = cursor.getBlob(cursor.getColumnIndex("HotelImage"));//读取字节流图片
            Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length);//字节流转换为点阵

            Tb_HotelInfo HotelInfo = new Tb_HotelInfo(HotelTel, HotelName, bmpout, HotelAddress, HotelScore);
            //构建新Test对象，图片为点阵类型
            list.add(HotelInfo);
        }
        return list;
    }


    @SuppressLint("Range")
    public List<Flight> Search_Flight(String str) {
        List<Flight> list = new ArrayList<Flight>();
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Flight where (FlightForm like '%"+str+"%') or (FlightTo like '%"+str+"%') or (FlightNumber like '%"+str+"%')",null);
        while(cursor.moveToNext()){
            Integer FlightNumber=cursor.getInt(cursor.getColumnIndex("FlightNumber"));
            String TakeoffTime=cursor.getString(cursor.getColumnIndex("TakeoffTime"));
            String SeatClass=cursor.getString(cursor.getColumnIndex("SeatClass"));
            Integer TicketPrice=cursor.getInt(cursor.getColumnIndex("TicketPrice"));
            String CompanyName=cursor.getString(cursor.getColumnIndex("CompanyName"));
            String LandingTime=cursor.getString(cursor.getColumnIndex("LandingTime"));
            String TotalHour=cursor.getString(cursor.getColumnIndex("TotalHour"));
            String FlightForm=cursor.getString(cursor.getColumnIndex("FlightForm"));
            String FlightTo=cursor.getString(cursor.getColumnIndex("FlightTo"));
            byte[] in=cursor.getBlob(cursor.getColumnIndex("CompanyImage"));//读取字节流图片
            Bitmap bmpout=BitmapFactory.decodeByteArray(in,0,in.length);//字节流转换为点阵

            Flight flight= new Flight(FlightNumber,TakeoffTime,SeatClass,TicketPrice,CompanyName,LandingTime,TotalHour,FlightForm,FlightTo,bmpout);//构建新Test对象，图片为点阵类型
            list.add(flight);

        }

        return list;



    }


    public void delete_moment(Integer id){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        db.execSQL("delete from moment where _id=?",new String[]{String.valueOf(id)});
        db.close();
    }
    public void delete_hotel(String hotelname){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        db.execSQL("delete from Tb_HotelInfo where HotelName=?",new String[]{hotelname});
        db.close();
    }

    public void delete_flight(Integer id){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        db.execSQL("delete from Flight where FlightNumber=?",new String[]{String.valueOf(id)});
        db.close();
    }



    public boolean userlogin(String UserName,String UserPassword){
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        String sql="select * from Tb_UserInfo where UserName=? and UserPassword=?";
        Cursor cursor=db.rawQuery(sql, new String[]{UserName, UserPassword});
        if(cursor.moveToFirst()==true){
            cursor.close();
            return true;
        }
        return false;
    }
    public boolean adminlogin(String AdminName,String AdminPassword){
        SQLiteDatabase db=dbhelper.getReadableDatabase();
        String sql="select * from Tb_AdminInfo where AdminName=? and AdminPassword=?";
        Cursor cursor=db.rawQuery(sql, new String[]{AdminName, AdminPassword});
        if(cursor.moveToFirst()==true){
            cursor.close();
            return true;
        }
        return false;
    }

    public boolean register(Tb_UserInfo user){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        String sql="insert into Tb_UserInfo(UserName,UserPassword,UserTel,UserAddress) values(?,?,?,?)";
        Object obj[]={user.getUserName(),user.getUserPassword(),user.getUserTel(),user.getUserAddress()};
        db.execSQL(sql, obj);
        return true;
    }

    public boolean adminregister(Tb_AdminInfo admin){
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        String sql="insert into Tb_AdminInfo(AdminName,AdminPassword) values(?,?)";
        Object obj[]={admin.getAdminName(),admin.getAdminPassword()};
        db.execSQL(sql, obj);
        return true;
    }
}

